package ee.siimoliver.chat.GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import ee.siimoliver.chat.GUI.dialogs.ChattableInputDialog;
import ee.siimoliver.chat.GUI.dialogs.NewChatDialog;
import ee.siimoliver.chat.controllers.BusinessLogicController;
import ee.siimoliver.chat.utils.Const;

public class MenuPanel extends JMenuBar implements Const {

	private static final long serialVersionUID = 1L;
	private JFrame parent;
	private JMenu fileMenu;
	private JMenuItem newChat;
	private JMenuItem exit;
	private JMenuItem properties;
	private JMenuItem searchChatmate;
	
	public MenuPanel(JFrame parent) {
		super();
		this.parent = parent;
		initFields();
		addProperties();
	}
	
	private void initFields() {
		fileMenu = new JMenu(LBL_FILE_MENU);
		newChat = new JMenuItem(LBL_NEW_CHAT, 
				new ImageIcon(IMAGE_ROOT + "ic_chat" + Const.IMAGE_EXT));
		exit = new JMenuItem(LBL_EXIT);
		properties = new JMenuItem(LBL_PROPERTIES);
		searchChatmate = new JMenuItem(LBL_SEARCH_CHATMATE, 
				new ImageIcon(IMAGE_ROOT + "ic_add_contact_holo_light" + Const.IMAGE_EXT));
		fileMenu.add(newChat);
		fileMenu.add(searchChatmate);
		fileMenu.addSeparator();
		fileMenu.add(properties);
		fileMenu.addSeparator();
		fileMenu.add(exit);
		add(fileMenu);
	}
	
	private void addProperties() {
		fileMenu.setMnemonic(KeyEvent.VK_F);
		newChat.addActionListener(new NewChatAction());
		searchChatmate.addActionListener(new SearchNewChatmateAction());
		exit.addActionListener(new ExitAction());
		properties.addActionListener(new PropertiesAction());
		newChat.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_DOWN_MASK));
		searchChatmate.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_DOWN_MASK));
		properties.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, InputEvent.ALT_DOWN_MASK));
	}
	
	private class SearchNewChatmateAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			ChattableInputDialog cid = new ChattableInputDialog(parent, Const.LBL_ENTER_NAME, 
					new ImageIcon(Const.IMAGE_ROOT + "ic_btn_search_big.png"));
			cid.display();
		}
	}
	
	private class NewChatAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			NewChatDialog ncd = new NewChatDialog(parent, 
					((ChatFrame) parent).getBusinessLogicController(), LBL_CHAT_APPLICATION);
			ncd.display();
		}
	}
	private class ExitAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			BusinessLogicController controller = ((ChatFrame) parent).getBusinessLogicController();
			if (controller != null) {
				controller.stopServer();
			}
			parent.dispose();
			System.exit(0);
		}
	}
	private class PropertiesAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			((ChatFrame) parent).showPropertiesDialog();
		}
	}
}
